package com.zjw.zy.heart.configuration;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import java.util.List;


@Configuration
@EnableJms
public class WechatMQConfiguration {


    @Value("${spring.activemq.broker-url}")
    public String url;
    @Value("${spring.activemq.username}")
    public String username;

    @Value("${spring.activemq.password}")
    public String password;
    @Value("${spring.activemq.queue-wechat}")
    public String wechatQueueName;

    @Value("${spring.activemq.white-packages}")
    public List<String> whitePackages;

    @Bean("WechatQueue")
    public Queue wechatQueue() {
        return new ActiveMQQueue(wechatQueueName);
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(username, password, url);
        //配置可序列化白名单
        //factory.setTrustedPackages(whitePackages);
        //todo 这里不安全，后面配置白名单
        factory.setTrustAllPackages(true);
        return factory;
    }

    @Bean("wechatListenerContainerFactoryQueue")
    public JmsListenerContainerFactory<?> jmsListenerContainerFactoryQueue(ConnectionFactory factory) {
        DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
        defaultJmsListenerContainerFactory.setConnectionFactory(factory);
        defaultJmsListenerContainerFactory.setPubSubDomain(false);//false代表队列
        return defaultJmsListenerContainerFactory;
    }
}
